"""
csv dataset
Row format: image_file_path box1 box2 ... boxN;
Box format: x_min,y_min,x_max,y_max,class_id (no space).
eg.
/data/imgs/img_001.jpg,837,346,981,456,cow
/data/imgs/img_002.jpg,215,312,279,391,cat 
/data/imgs/img_002.jpg,22,5,89,84,dog
/data/imgs/img_003.jpg,......
"""
import os
import numpy as np
from PIL import Image
from collections import defaultdict
import numpy as np


class CSVDataset(object):
    def __init__(self, csv_path, transforms=None):
        self.transforms = transforms
        self.image_annotations = self.load_data(csv_path)
        self.image_paths = self.image_annotations.keys()

    def __len__(self):
        return len(self.image_paths)

    def __getitem__(self, index):
        image_path = self.image_paths[index]
        annotations = self.image_annotations[image_path]
        image = Image.open(image_paths, mode='r')
        image = image.convert('RGB')

    
    def load_data(self, csv_path):
        data = defaultdict(list)
        with open(csv_path, 'r') as f:
            for line in f.readlines():
                vec = line.strip().split(',')
                image_path = vec[0]
                annot = vec[1:]
                data[image_path].append(annot)
        return data
